home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / dkbtrace / pbmplus / source / ppm / ppmtorgb.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-06  |  2.6 KB  |  99 lines

  1. /* ppmtorgb3.c - separate a portable pixmap into three portable graymaps
  2. **
  3. ** Copyright (C) 1991 by Jef Poskanzer.
  4. **
  5. ** Permission to use, copy, modify, and distribute this software and its
  6. ** documentation for any purpose and without fee is hereby granted, provided
  7. ** that the above copyright notice appear in all copies and that both that
  8. ** copyright notice and this permission notice appear in supporting
  9. ** documentation.  This software is provided "as is" without express or
  10. ** implied warranty.
  11. */
  12.  
  13. #include "ppm.h"
  14. #include "pgm.h"
  15.  
  16. void
  17. main( argc, argv )
  18.     int argc;
  19.     char* argv[];
  20.     {
  21.     FILE* ifp;
  22.     FILE* redfile;
  23.     FILE* grnfile;
  24.     FILE* blufile;
  25.     char* basename;
  26.     char filename[100];
  27.     char* cp;
  28.     pixel* pixelrow;
  29.     register pixel* pP;
  30.     gray* grayrow;
  31.     register gray* gP;
  32.     int rows, cols, format, row;
  33.     register int col;
  34.     pixval maxval;
  35.  
  36.     ppm_init( &argc, argv );
  37.  
  38.     if ( argc > 2 )
  39.     pm_usage( "[ppmfile]" );
  40.  
  41.     if ( argc == 2 )
  42.     {
  43.     ifp = pm_openr( argv[1] );
  44.     basename = argv[1];
  45.     cp = rindex( basename, '.' );
  46.     if ( cp != NULL )
  47.         *cp = '\0';
  48.     }
  49.     else
  50.     {
  51.     ifp = stdin;
  52.     basename = "noname";
  53.     }
  54.  
  55.     ppm_readppminit( ifp, &cols, &rows, &maxval, &format );
  56.     pixelrow = ppm_allocrow( cols );
  57.     (void) strcpy( filename, basename );
  58.     (void) strcat( filename, ".red" );
  59.     redfile = pm_openw( filename );
  60.     pgm_writepgminit( redfile, cols, rows, (gray) maxval, 0 );
  61.     (void) strcpy( filename, basename );
  62.     (void) strcat( filename, ".grn" );
  63.     grnfile = pm_openw( filename );
  64.     pgm_writepgminit( grnfile, cols, rows, (gray) maxval, 0 );
  65.     (void) strcpy( filename, basename );
  66.     (void) strcat( filename, ".blu" );
  67.     blufile = pm_openw( filename );
  68.     pgm_writepgminit( blufile, cols, rows, (gray) maxval, 0 );
  69.     grayrow = pgm_allocrow( cols );
  70.  
  71.     for ( row = 0; row < rows; ++row )
  72.     {
  73.     ppm_readppmrow( ifp, pixelrow, cols, maxval, format );
  74.  
  75.     for ( col = 0, pP = pixelrow, gP = grayrow; col < cols;
  76.           ++col, ++pP, ++gP )
  77.         *gP = (gray) PPM_GETR( *pP );
  78.     pgm_writepgmrow( redfile, grayrow, cols, maxval, 0 );
  79.  
  80.     for ( col = 0, pP = pixelrow, gP = grayrow; col < cols;
  81.           ++col, ++pP, ++gP )
  82.         *gP = (gray) PPM_GETG( *pP );
  83.     pgm_writepgmrow( grnfile, grayrow, cols, maxval, 0 );
  84.  
  85.     for ( col = 0, pP = pixelrow, gP = grayrow; col < cols;
  86.           ++col, ++pP, ++gP )
  87.         *gP = (gray) PPM_GETB( *pP );
  88.     pgm_writepgmrow( blufile, grayrow, cols, maxval, 0 );
  89.     }
  90.  
  91.     pm_close( ifp );
  92.     pm_close( redfile );
  93.     pm_close( blufile );
  94.     pm_close( grnfile );
  95.     pm_close (stdout);
  96.  
  97.     exit( 0 );
  98.     }
  99.